package cn.dubnation.car.service.impl;

import cn.dubnation.base.constants.SystemConstants;
import cn.dubnation.base.enums.ResponseCode;
import cn.dubnation.base.exception.BusinessException;
import cn.dubnation.car.domain.CarType;
import cn.dubnation.car.mapper.CarTypeMapper;
import cn.dubnation.car.service.ICarTypeService;
import cn.dubnation.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhu
 * @since 2024-01-03
 */
@Service
public class CarTypeServiceImpl extends BaseServiceImpl<CarType> implements ICarTypeService {

    @Autowired(required = false)
    private CarTypeMapper carTypeMapper;

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    /**
     * 获取车辆类型
     * @return
     */

    @Override
    public List<CarType> getTreeData() {
        return getTypesByLoop();
    }

    /**
     * 循环获取车辆类型-空间换取时间
     * @return
     */
    public List<CarType> getTypesByLoop() {
        //1.查询所有的车辆类型数据
        List<CarType> carTypes = carTypeMapper.queryAll();
        //2.定义数组用来存储返回的树形车辆类型数据list
        List<CarType> typeTreeList = new ArrayList<>();
        Map<Long, CarType> typesMap = new HashMap<>();
        //3.遍历获取的类型数组, 将类型id和对应的车辆类型数据存储到map中, 将车辆数据以属性结构的形式存储到list中返回
        carTypes.forEach(carType ->
            typesMap.put(carType.getId(), carType));
        for (CarType carType : carTypes) {
            List<CarType> childrenList = new ArrayList<>();
            for (CarType type : carTypes) {
                if (carType.equals(typesMap.get(type.getPid()))){
                    //找到类型父子级的关系
                    childrenList.add(type);
                }
            }
            //判断是否是顶级父类型
            if (carType.getPid().equals(0L)){
                typeTreeList.add(carType);
            }
            carType.setChildren(childrenList);
        }
        return typeTreeList;
    }

    /**
     * 删除车辆类型
     * @param typeId
     */
    @Override
    public void deleteType(Long typeId) {
        //1.根据车辆类型数据的description字段获取当前类型数据的所有子类型
        List<CarType> carTypeList = (List<CarType>) redisTemplate.opsForValue().get(SystemConstants.Redis.CAR_TYPE_TREE);
        try {
            if (Objects.isNull(carTypeList)){
                carTypeList = carTypeMapper.queryAll();
            }
            redisTemplate.delete(SystemConstants.Redis.CAR_TYPE_TREE);
            CarType deleteCarType = null;
            List<Long> deleteTypeIds = new ArrayList<>();
            for (CarType carType : carTypeList) {
                //获取每条数据的description字段, 将与该条数据description相关的数据抽取出来
                if (carType.getId().equals(typeId)){
                    //获取该条数据
                    deleteCarType = carType;
                }
                if (deleteCarType != null){
                    if (carType.getDescription().contains(deleteCarType.getDescription())){
                        //获取到该条数据的所有子类型数据
                        deleteTypeIds.add(carType.getId()); //将需要删除的车辆类型数据存储到集合中
                    }
                }
            }
            //2.删除数据
            carTypeMapper.batchDelete(deleteTypeIds);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResponseCode.RESPONSE_CODE_4000014);
        }
        redisTemplate.delete(SystemConstants.Redis.CAR_TYPE_TREE); //延时双删
    }
}
